package trans.action;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import trans.action.support.PageMode;
import trans.model.AirSa;
import trans.model.Gatway;
import trans.model.OceanSa;
import trans.model.Sa;
import trans.model.SaComment;
import trans.model.User;
import trans.service.GatwayDAO;
import trans.service.SADAO;
import trans.service.SaCommentDAO;
import trans.service.UserDAO;
import trans.support.BasicSupport;
import trans.support.Constants;
import trans.utils.HibernateUtil;

import com.opensymphony.xwork2.Preparable;

public class SAMgrAction extends BasicSupport implements Preparable{
	
	Map<Integer,String> usableStates = new LinkedHashMap<Integer,String>();
	String saType;
	Integer editSaId;
    OceanSa oceanSa;
	AirSa airSa;
    
	Integer member_mawbShipper;
	Integer member_mawbConsignee;
	Integer member_hawbShipper;
	Integer member_hawbConsignee;
	
	String stateChangeTime;
	String  relativeStation;
	String relativeFlightNo;
	
	
	//进入SA新建页面
	public  String create() throws Exception{
		if(Constants.SATYPE_AIR.equals(saType)){
			new AirSa().initStates(currentUser(), usableStates);
			return "airSaEdit";
		}else{
			new OceanSa().initStates(currentUser(),usableStates);
			return "oceanSaEdit";
		}
	}
	
	//编辑SA
	public   String editSa()  throws Exception{
		Sa sa = SADAO.getSa(editSaId);
		if(sa instanceof AirSa){
			airSa = (AirSa)sa;
		}
		if(sa instanceof OceanSa){
			oceanSa = (OceanSa)sa;
		}
		
		
		if(currentUser().getIsCustomer()){
			sa.prepareSaStateShow(request);
			sa.prepareTransNoShow(request);
			
			PageMode pm = getPageMode(request);
			pm.addParam(SaComment.PROP_SA_ID, this.getEditSaId());
			SaCommentDAO.fillCommentPage(pm);
			initPager(pm);
			
			if(sa.getFlagRead() == Constants.STATUS_SA_HANDED){
				sa.setFlagRead(Constants.STATUS_SA_NONE);
				HibernateUtil.saveOrUpdate(sa);
			}
			
			return sa.getViewName();
		}else{
			sa.updateMsgUser(currentUser());
			SADAO.updateSA(sa);
			sa.initStates(currentUser(), usableStates);
			
			request.setAttribute("isFreeze", sa.isFreeze(currentUser()));
			return sa.getEditName();
		}
	};
	
	public   String  saveAirSa() throws Exception{
		airSa.combinAwb();
			
			if(airSa.caculatePendding()){
				airSa.setIsPendding(Constants.STATUS_ENABLE);
			}else{
				airSa.setIsPendding(Constants.STATUS_DISABLE);
			}
			
			airSa.addNewRemark2Remark(currentUser().getUsername(),UserDAO.getSysusers(),currentUser());
			
			if(airSa.getId() == null ){
				SADAO.saveSA(airSa,currentUser());
				addActionMessage(getText("message.sa.create.success"));
			}else{
				SADAO.updateSA(airSa, currentUser());
				addActionMessage(getText("message.sa.update.success"));
			}
			
			//reload User for getting usrTip after update user
			airSa = (AirSa) SADAO.getSa(airSa.getId());
			
			airSa.saveTips(member_mawbShipper, member_mawbConsignee, member_hawbShipper, member_hawbConsignee);
			
			airSa.initStates(currentUser(), usableStates);
			request.setAttribute("isFreeze", airSa.isFreeze(currentUser()));
			return airSa.getEditName();
	};
	
	
	public String saveOceanSa() throws Exception {
		oceanSa.addNewRemark2Remark(currentUser().getUsername(),UserDAO.getSysusers(),currentUser());
		
		if(oceanSa.getId() == null ){
			SADAO.saveSA(oceanSa,currentUser());
			addActionMessage(getText("message.sa.create.success"));
		}else{
			SADAO.updateSA(oceanSa, currentUser());
			addActionMessage(getText("message.sa.update.success"));
		}
		
		//reload User for getting usrTip after update user
		oceanSa = (OceanSa) SADAO.getSa(oceanSa.getId());
		
		oceanSa.saveTips(member_mawbShipper, member_mawbConsignee, member_hawbShipper, member_hawbConsignee);
		
		oceanSa.initStates(currentUser(), usableStates);
		request.setAttribute("isFreeze", oceanSa.isFreeze(currentUser()));
		return oceanSa.getEditName();
	}
	
	public String addSAStateHisIndex()throws Exception {
		return "addSAStateHisIndex";
	}
	
	
	public String addSAStateHis()throws Exception {
		Date dateTime = DateUtils.parseDate(stateChangeTime,new String[]{"yyyy-MM-dd HH:mm"});
		
		SADAO.addSAStateHis(currentUser(), editSaId, dateTime, relativeStation, relativeFlightNo);
		stateChangeTime = null;
		relativeStation = "";
		relativeFlightNo = "";
		addActionMessage("add an new DLV change History sucess!");
		return addSAStateHisIndex();
	}
	


	private static final long serialVersionUID = 1L;
	Map<Integer,String> customers = new LinkedHashMap<Integer,String>();
	Map<Integer,String> usableGatways = new LinkedHashMap<Integer,String>();
	Map<Integer,String> flyAllowOption =  new LinkedHashMap<Integer,String>();
	Map<Integer,String> penddingStatus = new LinkedHashMap<Integer,String>();
	Map<String,String> saTypeList = new LinkedHashMap<String,String>();
	
	Map<String,String> authorizationOption =  new LinkedHashMap<String,String>();
	Map<String,String> termOption =  new LinkedHashMap<String,String>();

	Map<String,String> consolidationAllowedOption =  new LinkedHashMap<String,String>();
	
	Map<String,String> qtyOption = new LinkedHashMap<String, String>();
	
	@Override
	public void prepare() throws Exception {
		initManuShow();
		
		List<User> users = UserDAO.getCustomers();
		customers.put(null, "");
		for(User each : users){
			customers.put(each.getId(), each.getUserCode());
		}
		penddingStatus.put(null, "All");
		penddingStatus.put(Constants.STATUS_ENABLE, "PENDDING");
		penddingStatus.put(Constants.STATUS_DISABLE, "COMFIRMED");
		
		
		if(currentUser().getIsCustomer()){
			saTypeList.put(Constants.SATYPE_ALL, "ALL");
		}
		saTypeList.put(Constants.SATYPE_AIR, "AIR");
		saTypeList.put(Constants.SATYPE_OCEAN, "OCEAN");
		
		List<Gatway> gatways = GatwayDAO.getEnableGatway();
		usableGatways.put(null, "");
		for(Gatway each : gatways){
			usableGatways.put(each.getId(), each.getName() + "(" + each.getDirectCode() +")");
		}
		
		flyAllowOption.put(0, getText("label.no"));
		flyAllowOption.put(1, getText("label.yes"));
		
		authorizationOption.put("N", getText("label.no"));
		authorizationOption.put("Y", getText("label.yes"));
		
		termOption.put("FCA", "FCA");
		termOption.put("FOB", "FOB");
		termOption.put("CFR", "CFR");
		termOption.put("CIF", "CIF");
		termOption.put("EXW", "EXW");
		termOption.put("DDP", "DDP");
		termOption.put("DDU", "DDU");
		
		consolidationAllowedOption.put("N", getText("label.no"));
		consolidationAllowedOption.put("Y", getText("label.yes"));
		
		qtyOption.put("LCL", "LCL");
		qtyOption.put("20GP", "20GP");
		qtyOption.put("40GP", "40GP");
		qtyOption.put("40HQ", "40HQ");
	}
	
	Integer userId;
	String saNo;
	Integer isPendding;
	boolean showQuickLink;
	public String search()throws Exception{
		showQuickLink = true;
		
		PageMode pm = getPageMode(request);
		
		if(StringUtils.isBlank(saType)){
			saType = Constants.SATYPE_AIR;
		}
		
		String reuslt;
		if(currentUser().getIsCustomer()){
			pm.addParam(Sa.PROP_CLIENT_ID, currentUser().getId());
			pm.addParam(Sa.PROP_STATUS, Constants.STATUS_ENABLE);
			reuslt = "saSearchCus";
		}else{
			if(currentUser().getUserRole().equals(Constants.ROLE_SYSUSER)){
				pm.addParam(Sa.PROP_STATUS, Constants.STATUS_ENABLE);
			}
			reuslt = "saSearch";
		}
		
		pm.addParam(Sa.PROP_IS_PENDDING, isPendding);
		pm.addParam(Sa.PROP_SA_NO, saNo);
		pm.addParam(Sa.PROP_SATYPE, saType);
		SADAO.fillSaPage(pm);
		
		initSaList(pm,currentUser());
		
		if(pm.getDatas().size() == 1 && StringUtils.isEmpty(saNo) == false){
			this.editSaId = ((Sa)pm.getDatas().get(0)).getId();
			return this.editSa();
		}
		
		initPager(pm);
		
		request.setAttribute("searchUrl", "SaMgr_search.do");
		
		return reuslt;
	}
	
	private void initSaList(PageMode pm, User currentUser) {
		List<Object> datas = pm.getDatas();
		for(Object each : datas){
			Sa sa = (Sa)each;
			if(StringUtils.isEmpty(sa.getHaveMsgUser()) == false){
				String[] ids = sa.getHaveMsgUser().split(",");
				for(String eachId : ids){
					if(eachId.equals(currentUser.getId()+"")){
						sa.setHaveNewMsg(true);
					}
				}
				
			};
			
		}
		
	}

	public String needHandSa(){
		PageMode pm = getPageMode(request);
		pm.addParam(Sa.PROP_STATUS, Constants.STATUS_ENABLE);
		pm.addParam(Sa.PROP_FLAG_READ, Constants.STATUS_SA_NEEDHAND);
		pm.addParam(Sa.PROP_SATYPE, saType);
		
		SADAO.fillSaPageByHandStatus(pm);
		initPager(pm);
		
		String reuslt;
		if(currentUser().getIsCustomer()){
			reuslt = "saSearchCus";
		}else{
			reuslt = "saSearch";
		}
		
		request.setAttribute("searchUrl", "SaMgr_needHandSa.do");
		return reuslt;
	}
	
	public String handedSa(){
		PageMode pm = getPageMode(request);
		pm.addParam(Sa.PROP_STATUS, Constants.STATUS_ENABLE);
		pm.addParam(Sa.PROP_FLAG_READ, Constants.STATUS_SA_HANDED);
		pm.addParam(Sa.PROP_CLIENT_ID, currentUser().getId());
		pm.addParam(Sa.PROP_SATYPE, saType);
		
		SADAO.fillSaPageByHandStatus(pm);
		initPager(pm);
		
		String reuslt;
		if(currentUser().getIsCustomer()){
			reuslt = "saSearchCus";
		}else{
			reuslt = "saSearch";
		}
		
		request.setAttribute("searchUrl", "SaMgr_handedSa.do");
		return reuslt;
	}
	
	
	
	private Integer updateStatus;
	public String updateSaStatus() throws Exception{
		SADAO.updateSaStatus(editSaId, updateStatus);
		addActionMessage(getText("message.sa.update.success"));
		return editSa();
	}
	
	
	SaComment saComment;
	public String addComment() throws Exception{
		saComment.setCreateBy_id(currentUser().getId());
		SaCommentDAO.addComment(saComment,currentUser().getIsCustomer());
		//need reset page data
		
		saComment.setComment("");
		addActionMessage(getText("message.sacomment.create.success"));
		return editSa();
	}
	
	
	public Integer getEditSaId() {
		return editSaId;
	}

	public void setEditSaId(Integer editSaId) {
		this.editSaId = editSaId;
	}

	public Integer getUpdateStatus() {
		return updateStatus;
	}

	public void setUpdateStatus(Integer updateStatus) {
		this.updateStatus = updateStatus;
	}

	public Map<Integer, String> getCustomers() {
		return customers;
	}

	public void setCustomers(Map<Integer, String> customers) {
		this.customers = customers;
	}

	public Map<Integer, String> getUsableStates() {
		return usableStates;
	}

	public void setUsableStates(Map<Integer, String> usableStates) {
		this.usableStates = usableStates;
	}


	public Map<Integer, String> getUsableGatways() {
		return usableGatways;
	}

	public void setUsableGatways(Map<Integer, String> usableGatways) {
		this.usableGatways = usableGatways;
	}

	public Map<Integer, String> getFlyAllowOption() {
		return flyAllowOption;
	}

	public void setFlyAllowOption(Map<Integer, String> flyAllowOption) {
		this.flyAllowOption = flyAllowOption;
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public String getSaNo() {
		return saNo;
	}

	public void setSaNo(String saNo) {
		this.saNo = saNo;
	}

	public Map<Integer, String> getPenddingStatus() {
		return penddingStatus;
	}

	public void setPenddingStatus(Map<Integer, String> penddingStatus) {
		this.penddingStatus = penddingStatus;
	}

	public Integer getIsPendding() {
		return isPendding;
	}

	public void setIsPendding(Integer isPendding) {
		this.isPendding = isPendding;
	}

	public SaComment getSaComment() {
		return saComment;
	}

	public void setSaComment(SaComment saComment) {
		this.saComment = saComment;
	}

	public Map<String, String> getSaTypeList() {
		return saTypeList;
	}

	public void setSaTypeList(Map<String, String> saTypeList) {
		this.saTypeList = saTypeList;
	}

	public String getSaType() {
		return saType;
	}

	public void setSaType(String saType) {
		this.saType = saType;
	}

	public boolean isShowQuickLink() {
		return showQuickLink;
	}

	public void setShowQuickLink(boolean showQuickLink) {
		this.showQuickLink = showQuickLink;
	}


	public Integer getMember_mawbShipper() {
		return member_mawbShipper;
	}

	public void setMember_mawbShipper(Integer member_mawbShipper) {
		this.member_mawbShipper = member_mawbShipper;
	}

	public Integer getMember_mawbConsignee() {
		return member_mawbConsignee;
	}

	public void setMember_mawbConsignee(Integer member_mawbConsignee) {
		this.member_mawbConsignee = member_mawbConsignee;
	}

	public Integer getMember_hawbShipper() {
		return member_hawbShipper;
	}

	public void setMember_hawbShipper(Integer member_hawbShipper) {
		this.member_hawbShipper = member_hawbShipper;
	}

	public Integer getMember_hawbConsignee() {
		return member_hawbConsignee;
	}

	public void setMember_hawbConsignee(Integer member_hawbConsignee) {
		this.member_hawbConsignee = member_hawbConsignee;
	}

	public OceanSa getOceanSa() {
		return oceanSa;
	}

	public void setOceanSa(OceanSa oceanSa) {
		this.oceanSa = oceanSa;
	}

	public AirSa getAirSa() {
		return airSa;
	}

	public void setAirSa(AirSa airSa) {
		this.airSa = airSa;
	}


	public String getStateChangeTime() {
		return stateChangeTime;
	}

	public void setStateChangeTime(String stateChangeTime) {
		this.stateChangeTime = stateChangeTime;
	}

	public String getRelativeStation() {
		return relativeStation;
	}

	public void setRelativeStation(String relativeStation) {
		this.relativeStation = relativeStation;
	}

	public String getRelativeFlightNo() {
		return relativeFlightNo;
	}

	public void setRelativeFlightNo(String relativeFlightNo) {
		this.relativeFlightNo = relativeFlightNo;
	}

	public Map<String, String> getAuthorizationOption() {
		return authorizationOption;
	}

	public void setAuthorizationOption(Map<String, String> authorizationOption) {
		this.authorizationOption = authorizationOption;
	}

	public Map<String, String> getTermOption() {
		return termOption;
	}

	public void setTermOption(Map<String, String> termOption) {
		this.termOption = termOption;
	}

	public Map<String, String> getConsolidationAllowedOption() {
		return consolidationAllowedOption;
	}

	public void setConsolidationAllowedOption(
			Map<String, String> consolidationAllowedOption) {
		this.consolidationAllowedOption = consolidationAllowedOption;
	}

	public Map<String, String> getQtyOption() {
		return qtyOption;
	}

	public void setQtyOption(Map<String, String> qtyOption) {
		this.qtyOption = qtyOption;
	}
	
	
	
}
